﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib.WorldMap;
using System.Xml;
using GameLib.Serialization;
using System.Reflection;
using GameLib.Main;

namespace Sandbox.Serialization.XmlSerializers
{
	/*
	public class MapTilesSerializer : XmlSerializer<MapTileCollection>
	{
		protected override object DeserializeInnerTyped(MapTileCollection obj, Type type, XmlReader reader, DataFlags dataFlags)
		{
			if (reader.NodeType != XmlNodeType.Text)
				throw new Exception(string.Format("MapTilesSerializer can only read string element, current: {0}={1}, type:{2}", reader.Name, reader.Value, reader.NodeType));

			string[] rows = reader.Value.Split(',');

			if (rows.Length == 0)
			{
				obj.Map.Init(0, 0);
			}
			else
			{
				int w = rows[0].Trim('\n').Length;
				int h = rows.Length;

				obj.Map.Init(w, h);

				for (int y = 0; y < h; y++)
				{
					string row = rows[y].Trim('\n');
					if (row.Length != w)
						throw new Exception(string.Format("Tile def error: each row must be aligned, expect length {0}, current: {1} {2}", w, row.Length, row));

					for (int x = 0; x < w; x++)
					{
						char c = row[x];
						if (!char.IsWhiteSpace(c) && obj[x, y] != null)
						{
							obj[x, y].Terrain = Core.Play.World.Terrains.Find(c);
						}
					}
				}
			}

			// read out this string
			reader.Read();
			reader.MoveToContent();

			return obj;
		}

		protected override void SerializeInnerTyped(MapTileCollection obj, List<PropertyInfo> props, XmlWriter writer, DataFlags dataFlags)
		{
			StringBuilder sb = new StringBuilder(obj.Height * (obj.Width + 1));

			for (int y = 0; y < obj.Height; y++)
			{
				for (int x = 0; x < obj.Width; x++)
				{
					Tile tile = obj[x, y];
					sb.Append(tile != null ? tile.Terrain.Id : ' ');					
				}

				if (y < obj.Height - 1)
					sb.Append(",\n");
			}
			writer.WriteString(sb.ToString());
		}
	}*/
}
